package com.laxcen.smoke.dao;


import com.laxcen.smoke.dto.res.OperaterecordExtraRes;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface OperationDao {

    @Insert("insert into operaterecord values(#{id},#{smartno},#{boxno},#{opid},#{opname},#{optime},#{opendtime},#{optype},#{video},#{orderno},#{remark})")
    int insert(String id, String smartno, String boxno, String opid, String opname, Date optime, Date opendtime, int optype, String video, String orderno, String remark);

    @Insert("insert into operaterecorddetailtable values(#{id},#{recordid},#{smartno},#{boxno},#{optime},#{optype},#{opcontent},#{opresult},#{remark})")
    int insertdetail(String id, String recordid, String smartno, String boxno, Date optime, int optype, String opcontent, int opresult, String remark);

    @Delete("delete from operaterecord where id = #{id}")
    int delete(String id);

    @Delete("delete from operaterecorddetailtable where recordid = #{id} ")
    int deletedetail(String id);

//    @Update("")

    @Select("select f_id from sys_user where f_username = #{username}")
    String getUserNameById(String username);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo = sm.no order by op.optime desc ")
    List<OperaterecordExtraRes> getAllList();

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo = sm.no where sm.no in (select distinct sm.no from smart sm right join real_smart_smartboard rss on sm.id = rss.SmartId where sm.address like CONCAT('%',#{place},'%')) order by op.optime desc")
    List<OperaterecordExtraRes> getAllListByRole(String place);

//    @Select("select distinct sm.no from smart sm right join real_smart_smartboard rss on sm.id = rss.SmartId where sm.address like CONCAT('%',#{place},'%') ")
//    List<String> getSmartByPCA(String place);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo  = sm.no  where op.orderno = #{orderno} order by op.optime desc")
    List<OperaterecordExtraRes> getListByOrder(String orderno);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo  = sm.no  where  sm.no in (select distinct sm.no from smart sm right join real_smart_smartboard rss on sm.id = rss.SmartId where sm.address like CONCAT('%',#{place},'%')) and op.orderno = #{orderno} order by op.optime desc")
    List<OperaterecordExtraRes> getListByOrderRole(String orderno, String place);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo = sm.no where op.id = #{id} order by op.optime desc ")
    OperaterecordExtraRes getOneById(String id);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo = sm.no  where op.optime between #{starttime} and #{endtime} order by op.optime desc")
    List<OperaterecordExtraRes> getListByOtime(Date starttime, Date endtime);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo = sm.no  where op.optime between #{starttime} and #{endtime} and sm.no in (select distinct sm.no from smart sm right join real_smart_smartboard rss on sm.id = rss.SmartId where sm.address like CONCAT('%',#{place},'%')) order by op.optime desc")
    List<OperaterecordExtraRes> getListByOtimeRole(Date starttime, Date endtime, String place);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo = sm.no  where op.optime between #{starttime} and #{endtime} and  sm.no in (select distinct sm.no from smart sm right join real_smart_smartboard rss on sm.id = rss.SmartId where sm.address like CONCAT('%',#{place},'%'))and op.orderno = #{orderno} order by op.optime desc")
    List<OperaterecordExtraRes> getListByOtimeandOrderRole(Date starttime, Date endtime, String orderno, String place);

    @Select("select op.*,sm.address address,sm.devicename devicename from operaterecord op left join smart sm on op.SmartNo = sm.no  where op.optime between #{starttime} and #{endtime} and op.orderno = #{orderno} order by op.optime desc")
    List<OperaterecordExtraRes> getListByOtimeandOrder(Date starttime, Date endtime, String orderno);

}
